﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cloud.Boss.Models;
using Cloud.Core.Data;
using Cloud.Core.Service;
using Cloud.Dto;
using Cloud.Framework.Controller;
using Cloud.Framework.Model;
using Cloud.Service.Log;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Options;

namespace Cloud.Boss.Controllers
{
    public class BaseController : SuperController
    {
        protected readonly IOptions<ProjectSetting> _projectSetting;
        protected readonly ILogService _logService;

        public BaseController(IOptions<ProjectSetting> _projectSetting, ILogService _logService)
        {
            this._projectSetting = _projectSetting;
            this._logService = _logService;
        }
        public override RequestLogModel WriteLog(ActionExecutingContext context)
        {
            var requestLogModel = base.WriteLog(context);
            if (requestLogModel != null)
            {
                requestLogModel.SystemName = _projectSetting.Value.SystemName;
                //TODO 写日志
                RequestLogDto requestLogDto = new RequestLogDto()
                {
                    SystemName = requestLogModel.SystemName,
                    ActionName = requestLogModel.ActionName,
                    ControllerName = requestLogModel.ControllerName,
                    CreateTime = requestLogModel.CreateTime,
                    HttpStatusCode = requestLogModel.HttpStatusCode,
                    IpAddress = requestLogModel.IpAddress,
                    LoginId = requestLogModel.LoginId,
                    LoginName = requestLogModel.LoginName,
                    Method = requestLogModel.Method,
                    Parameter = requestLogModel.Parameter,
                    Url = requestLogModel.Url
                };
                _logService.WriteRequestLog(requestLogDto);
               Core.Service.LogServiceHelper.Intance.Write(new LogModel() { CreatedTime = DateTime.Now, Dir = "request", Msg = requestLogDto.Url + "?" + requestLogDto.Parameter, Operator = requestLogDto.LoginName });
            }

            return requestLogModel;
        }

        protected ActionResult PagedList<T>(PageList<T> data) where T : class, new()
        {
           
            ViewBag.rows = data.TotalRows;
            ViewBag.pageSize = data.PageSize;
            ViewBag.pageCount = data.TotalPages;
            ViewBag.pageIndex = data.PageIndex;
            return View(data.ToList());
        }
    }
}